package com.cloud.configure;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TaskXxlConfiguration {

    private Logger logger = LoggerFactory.getLogger(TaskXxlConfiguration.class);

    // XXL-JOB服务端地址
    @Value("${task.xxl.admin.address}")
    private String adminAddress;

    // 执行程序应用名称
    @Value("${task.xxl.executor.app-name}")
    private String appName;

    // 执行程序注册表地址:默认使用注册表地址,否则如果地址为空,则使用IP:端口
    @Value("${task.xxl.executor.address}")
    private String address;

    // 执行程序服务器IP
    @Value("${task.xxl.executor.host}")
    private String host;

    // 执行程序服务器端口
    @Value("${task.xxl.executor.port}")
    private int port;

    // 访问令牌
    @Value("${task.xxl.access-token}")
    private String accessToken;

    // 执行程序日志路径
    @Value("${task.xxl.executor.log-path}")
    private String logPath;
    // 执行程序日志保留天数
    @Value("${task.xxl.executor.log-retention-days}")
    private int logRetentionDays;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>> XXL-JOB配置初始化...");
        // init executor
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddress);
        xxlJobSpringExecutor.setAppname(appName);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setIp(host);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }

}
